
const TicketsService = require('../services/ticketsService');

// 获取所有可购买的门票信息
exports.getTicketsList = async (ctx) => {
  try {
    const { scenic_id, category, page, limit } = ctx.query;
    const tickets = await TicketsService.getTicketsList(scenic_id, category, page, limit);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "获取成功",
      data: tickets
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "获取失败",
      error: error.message
    };
  }
};

// 获取单个票务详情
exports.getTicketById = async (ctx) => {
  try {
    const { id } = ctx.params;
    const ticket = await TicketsService.getTicketById(id);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "获取成功",
      data: ticket
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "获取失败",
      error: error.message
    };
  }
};

// 用户购买门票
exports.createOrder = async (ctx) => {
  try {
    const orderData = ctx.request.body;
    const order = await TicketsService.createOrder(orderData);
    ctx.status = 201;
    ctx.body = {
      code: 201,
      message: "订单创建成功",
      data: { order_id: order.order_id }
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "创建失败",
      error: error.message
    };
  }
};

// 查询用户订单
exports.getUserOrders = async (ctx) => {
  try {
    const { user_id } = ctx.params;
    const orders = await TicketsService.getUserOrders(user_id);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "获取成功",
      data: orders
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "获取失败",
      error: error.message
    };
  }
};

// 支付订单
exports.payOrder = async (ctx) => {
  try {
    const { order_id, user_id, payment_method } = ctx.request.body;
    const order = await TicketsService.payOrder(order_id, user_id, payment_method);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "支付成功",
      data: order
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "支付失败",
      error: error.message
    };
  }
};

// 取消订单
exports.cancelOrder = async (ctx) => {
  try {
    const { order_id, user_id } = ctx.request.body;
    const order = await TicketsService.cancelOrder(order_id, user_id);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "订单取消成功",
      data: order
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "取消失败",
      error: error.message
    };
  }
};

// 退款申请
exports.refundOrder = async (ctx) => {
  try {
    const { order_id, user_id, reason } = ctx.request.body;
    const order = await TicketsService.refundOrder(order_id, user_id, reason);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "退款申请成功",
      data: order
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "退款申请失败",
      error: error.message
    };
  }
};

// 更新票务库存
exports.updateTicketStock = async (ctx) => {
  try {
    const { id } = ctx.params;
    const { stock } = ctx.request.body;
    const result = await TicketsService.updateTicketStock(id, stock);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: result.message,
      data: result
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "更新失败",
      error: error.message
    };
  }
};
